package com.heatup.apt.common.util;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: qujia
 * Date: 2016/07/20
 * Time: 10:09
 */

public class DateUtil {
    private static final String[] WEEK_NAME = {"周日", "周一", "周二", "周三", "周四", "周五",
            "周六"};

    public static void main(String[] args) {
        System.out.println(isInAfterDays("2012-05-27", 30));
        System.out.println(getDateBeforeNow(10));
        System.out.println(calSencond(300));

    }

    public static String getHourAndMinuteString(Date date) {
        DateFormat df = new SimpleDateFormat("HH:mm");

        return df.format(date);
    }

    public static int getMinute(String time) {
        String[] sarr = time.split(":");
        int h = Integer.valueOf(sarr[0]);
        int m = Integer.valueOf(sarr[1]);
        return h * 60 + m;
    }


    /**
     * 天白还是晚上
     *
     * @param time
     * @return -1：参数不合法，1：白天，2：晚上
     */
    public static int isDayOrNight(String time) {
        if (time == null || time.length() == 0)
            return -1;
        if (!time.contains(":"))
            return -1;

        String[] ss = time.split(":");
        if (ss.length != 3)
            return -1;
        if (Integer.valueOf(ss[0]) < 12)
            return 1;
        else
            return 0;
    }

    public static boolean isOldTime(String dateTime) {
        return isOldTime(dateTime, "yyyy-MM-dd hh:mm:ss");
    }

    public static boolean isOldTime(String dateTime, String pattern) {
        if (StringUtils.isBlank(pattern))
            pattern = "yyyy-MM-dd";
        DateFormat df = new SimpleDateFormat(pattern);
        try {
            Date d = df.parse(dateTime);
            Date now = new Date();
            return now.after(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    //是否在现在到未来几天内
    public static boolean isInAfterDays(String date, int size) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d = df.parse(date);
            long t = d.getTime();
            long now = System.currentTimeMillis();
            return t - now < 86400000L * size;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    //在过去时间几天内
    public static boolean isInBeforeDays(Date date, int size) {
        long t = date.getTime();
        long now = System.currentTimeMillis();
        return now - t < 86400000L * size;
    }

    public static String formatDateString(String date) {
        String ds[] = date.split("-");
        String result = null;
        if (ds.length == 3) {
            result = Integer.valueOf(ds[1]) + "月" + Integer.valueOf(ds[2]) + "日";
        }
        return result;
    }


    public static String getWeekNameOfDateAndToday(String date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String result = null;
        try {
            Date d = df.parse(date);
            int index = getDateOfWeek(d);
            int today = getDateOfWeek(new Date());
            if (index == today)
                result = "今天";
            else
                result = WEEK_NAME[index];
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getWeekNameOfDate2(String date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String result = null;
        try {
            Date d = df.parse(date);
            int index = getDateOfWeek(d);
            int today = getDateOfWeek(new Date());
            if (index == today)
                result = "今天";
            else if (index == today + 1)
                result = "明天";
            else
                result = WEEK_NAME[index];
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getWeekNameOfDate(String date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String result = null;
        try {
            Date d = df.parse(date);
            int index = getDateOfWeek(d);
            result = WEEK_NAME[index];
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int getDateOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return index;
    }

    public static int getTodayOfWeek() {
        return getDateOfWeek(null);
    }

    public static String[] getNextWeekDays(String pattern) {
        int index = getTodayOfWeek();
        int t = 7 - index;
        Date date = getDateAfterToday(t + 1);
        String dates[] = getDatesAfterDate(date, 7, pattern);
        return dates;
    }

    public static String[] getCurrentWeekDays(String pattern) {
        String dates[] = new String[7];
        int index = getTodayOfWeek();
        int before = index;
        int after = 7 - index;
        DateFormat df = new SimpleDateFormat(pattern);
        int i = 0;
        for (; i < before; i++) {
            Date d = getDateBeforeToday(before - i - 1);
            dates[i] = df.format(d);
        }
        for (int j = 1; j <= after; i++, j++) {
            Date d = getDateAfterToday(j);
            dates[i] = df.format(d);
        }
        return dates;
    }

    /**
     * 今天之后几天
     *
     * @param quantity 天数
     * @return
     */
    public static Date getDateAfterToday(int quantity) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, quantity);
        return cal.getTime();
    }

    public static Date getDateAfterDate(Date date, int quantity) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, quantity);
        return cal.getTime();
    }

    /**
     * 今天之前几天
     *
     * @param quantity 天数
     * @return
     */
    public static Date getDateBeforeToday(int quantity) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -quantity);
        return cal.getTime();
    }

    /**
     * 当前时间之前的几分钟
     *
     * @param quantityMinute
     * @return
     */
    public static Date getDateBeforeNow(int quantityMinute) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, -quantityMinute);
        return cal.getTime();
    }

    /**
     * 计算两个时间相差天数
     *
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 从今天开始之后几天，含今天
     *
     * @param quantity
     * @param pattern
     * @return
     */
    public static String[] getDatesAfterToday(int quantity, String pattern) {
        if (isBlank(pattern)) {
            pattern = "yyyy-MM-dd";
        }
        int count = Math.abs(quantity);
        String dates[] = new String[count];
        DateFormat df = new SimpleDateFormat(pattern);
        for (int i = 0; i < count; i++) {
            Date d = getDateAfterToday(i);
            dates[i] = df.format(d);
        }
        return dates;
    }

    public static String[] getDatesAfterDate(Date date, int quantity,
                                             String pattern) {
        if (isBlank(pattern)) {
            pattern = "yyyy-MM-dd";
        }
        int count = Math.abs(quantity);
        String dates[] = new String[count];
        DateFormat df = new SimpleDateFormat(pattern);
        for (int i = 0; i < count; i++) {
            Date d = getDateAfterDate(date, i);
            dates[i] = df.format(d);
        }
        return dates;
    }

    /**
     * 从今天开始之前几天，含今天
     *
     * @param quantity
     * @param pattern
     * @return
     */
    public static String[] getDatesBeforeToday(int quantity, String pattern) {
        if (isBlank(pattern)) {
            pattern = "yyyy-MM-dd";
        }
        int count = Math.abs(quantity);
        String dates[] = new String[count];
        DateFormat df = new SimpleDateFormat(pattern);
        for (int i = 0; i < count; i++) {
            Date d = getDateBeforeToday(i);
            dates[i] = df.format(d);
        }
        return dates;
    }

    /**
     * 当前小时
     *
     * @return
     */
    public static int getNowHour() {
        Date d = new Date();
        int hours = d.getHours();
        return hours;
    }


    public static final int FMT_DATE_YYYY = 0;
    public static final int FMT_DATE_YYYYMMDD = 1;
    public static final int FMT_DATE_YYYYMMDD_HHMMSS = 2;
    public static final int FMT_DATE_HHMMSS = 3;
    public static final int FMT_DATE_HHMM = 4;
    public static final int FMT_DATE_SPECIAL = 5;
    public static final int FMT_DATE_MMDD = 6;
    public static final int FMT_DATE_YYYYMMDDHHMM = 7;
    public static final int FMT_DATE_MMDD_HHMM = 8;
    public static final int FMT_DATE_MMMDDD = 9;
    public static final int FMT_DATE_YYYY_MM_DD = 10;
    public static final int FMT_DATE_YYYY_MM_DD_HH = 11;
    public static final int FMT_DATE_YYYYMMDDHHMMSS = 12;
    public static final int FMT_DATE_YYMMDD = 13;
    public static final int FMT_DATE_HH = 100;
    public static final int FMT_DATE_MMDDE = 101;
    public static final int FMT_DATE_YYYYMMDDHH = 102;
    public static final int FMT_DATE_MMDD_HH = 103;
    public static final int FMT_DATE_YYYYdoMMdoDD = 104;

    public static String formatDate(Date date, int nFmt) {
        SimpleDateFormat fmtDate = new SimpleDateFormat();
        switch (nFmt) {
            default:
            case FMT_DATE_YYYY:
                fmtDate.applyLocalizedPattern("yyyy");
                break;
            case FMT_DATE_YYYYMMDD:
                fmtDate.applyPattern("yyyy-MM-dd");
                break;
            case FMT_DATE_YYYYMMDD_HHMMSS:
                fmtDate.applyPattern("yyyy-MM-dd HH:mm:ss");
                break;
            case FMT_DATE_HH:
                fmtDate.applyPattern("HH");
                break;
            case FMT_DATE_HHMM:
                fmtDate.applyPattern("HH:mm");
                break;
            case FMT_DATE_HHMMSS:
                fmtDate.applyPattern("HH:mm:ss");
                break;
            case FMT_DATE_SPECIAL:
                fmtDate.applyPattern("yyyyMMdd");
                break;
            case FMT_DATE_MMDD:
                fmtDate.applyPattern("MM-dd");
                break;
            case FMT_DATE_YYYYMMDDHHMM:
                fmtDate.applyPattern("yyyy-MM-dd HH:mm");
                break;
            case FMT_DATE_YYYYMMDDHH:
                fmtDate.applyPattern("yyyy-MM-dd HH");
                break;
            case FMT_DATE_MMDD_HHMM:
                fmtDate.applyPattern("MM-dd HH:mm");
                break;
            case FMT_DATE_MMDD_HH:
                fmtDate.applyPattern("MM-dd HH");
                break;
            case FMT_DATE_MMMDDD:
                fmtDate.applyPattern("MM月dd日");
                break;
            case FMT_DATE_MMDDE:
                fmtDate.applyPattern("MM-dd(E)");
                break;
            case FMT_DATE_YYYY_MM_DD:
                fmtDate.applyPattern("yyyy_MM_dd");
                break;
            case FMT_DATE_YYYY_MM_DD_HH:
                fmtDate.applyPattern("yyyy_MM_dd_HH");
                break;
            case FMT_DATE_YYMMDD:
                fmtDate.applyPattern("yyMMdd");
                break;
            case FMT_DATE_YYYYMMDDHHMMSS:
                fmtDate.applyPattern("yyyyMMddHHmmss");
                break;
            case FMT_DATE_YYYYdoMMdoDD:
                fmtDate.applyPattern("yyyy.MM.dd");
                break;
        }
        return fmtDate.format(date);
    }

    public static Date parseUtilDate(String strDate, int nFmtDate) {
        if (strDate == null || strDate.trim().length() == 0) return null;
        SimpleDateFormat fmtDate = null;
        switch (nFmtDate) {
            default:
            case FMT_DATE_YYYYMMDD:
                if (strDate.length() == 4) {
                    strDate = strDate + "-00-00";
                }
                if (strDate.length() == 7) {
                    strDate = strDate + "-00";
                }
                fmtDate = new SimpleDateFormat("yyyy-MM-dd");
                break;
            case FMT_DATE_YYYYMMDD_HHMMSS:
                fmtDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case FMT_DATE_HHMM:
                fmtDate = new SimpleDateFormat("HH:mm");
                break;
            case FMT_DATE_HHMMSS:
                fmtDate = new SimpleDateFormat("HH:mm:ss");
                break;
            case FMT_DATE_YYYY_MM_DD:
                fmtDate = new SimpleDateFormat("yyyy_MM_dd");
                break;
            case FMT_DATE_YYYY_MM_DD_HH:
                fmtDate = new SimpleDateFormat("yyyy_MM_dd_HH");
                break;
            case FMT_DATE_SPECIAL:
                fmtDate = new SimpleDateFormat("yyyyMMdd");
                break;
            case FMT_DATE_YYYYMMDDHHMMSS:
                fmtDate = new SimpleDateFormat("yyyyMMddHHmmss");
                break;
            case FMT_DATE_HH:
                fmtDate = new SimpleDateFormat("HH");
                break;
            case FMT_DATE_YYYYMMDDHH:
                fmtDate = new SimpleDateFormat("yyyy-MM-dd HH");
                break;
            case FMT_DATE_YYYYMMDDHHMM:
                fmtDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                break;
            case FMT_DATE_YYYYdoMMdoDD:
                fmtDate = new SimpleDateFormat("yyyy.MM.dd");
                break;
        }
        try {
            return fmtDate.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static boolean isBlank(String text) {
        return text == null || "".equals(text);
    }

    /**
     * 将秒数转换成分秒
     *
     * @param durationSeconds
     * @return
     */
    public static String calSencond(int durationSeconds) {
        int hours = durationSeconds / (60 * 60);
        int leftSeconds = durationSeconds % (60 * 60);
        int minutes = durationSeconds / 60;
        int seconds = leftSeconds % 60;

        StringBuffer sBuffer = new StringBuffer();
        //sBuffer.append(addZeroPrefix(hours));
        //sBuffer.append(":");
        sBuffer.append(addZeroPrefix(minutes));
        sBuffer.append(":");
        sBuffer.append(addZeroPrefix(seconds));

        return sBuffer.toString();
    }

    public static String addZeroPrefix(int number) {
        if (number < 10) {
            return "0" + number;
        } else {
            return "" + number;
        }

    }

    /**
     * 日期是否为今天
     *
     * @param date
     * @return
     */
    public static boolean isToday(Date date) {
        Date today = new Date();
        return (today.getYear() == date.getYear())
                && (today.getMonth() == date.getMonth())
                && (today.getDay() == date.getDay());
    }

    /**
     * XX分钟后的时间
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Date addMinites(Date date, int minutes) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minutes);
        return cal.getTime();
    }


    /**
     * 数字转换为大写
     *
     * @param num
     * @return
     */
    public static String numToUpper(int num) {
        // String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        String u[] = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        char[] str = String.valueOf(num).toCharArray();
        String rstr = "";
        for (int i = 0; i < str.length; i++) {
            rstr = rstr + u[Integer.parseInt(str[i] + "")];
        }
        return rstr;
    }
}
